﻿using Cube.Data;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;

namespace CubeForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Cube.Data.Cube cube = new Cube.Data.Cube();
        ProjectPainter project_painter;
        CubePainter cube_painter;
        PattenManager patten;
        private void Button1_Click(object sender, EventArgs e)
        {
            button1.Text = "重置";
            cube = new Cube.Data.Cube();
            project_painter = new ProjectPainter(cube, pictureBox1.CreateGraphics());
            cube_painter = new CubePainter(cube, pictureBox2.CreateGraphics());
            patten = new PattenManager(cube);
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
            button2.Enabled = button4.Enabled = button5.Enabled = button6.Enabled = button7.Enabled = button8.Enabled
                = button9.Enabled = button10.Enabled = button11.Enabled = button12.Enabled = button13.Enabled = button14.Enabled
                = button15.Enabled = button16.Enabled = button17.Enabled = button18.Enabled = button19.Enabled = button20.Enabled
                = button21.Enabled = button22.Enabled = button23.Enabled = button24.Enabled = button25.Enabled = button26.Enabled
                = button3.Enabled = true;
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            patten.U();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            patten._U();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button4_Click(object sender, EventArgs e)
        {
            patten.R();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button5_Click(object sender, EventArgs e)
        {
            patten._R();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button6_Click(object sender, EventArgs e)
        {
            patten.F();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button7_Click(object sender, EventArgs e)
        {
            patten._F();
            project_painter.Draw(); cube_painter.Draw();GC.Collect();
        }

        private void Button8_Click(object sender, EventArgs e)
        {
            patten.D();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button9_Click(object sender, EventArgs e)
        {
            patten._D();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button10_Click(object sender, EventArgs e)
        {
            patten.L();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button11_Click(object sender, EventArgs e)
        {
            patten._L();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button12_Click(object sender, EventArgs e)
        {
            patten.B();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button13_Click(object sender, EventArgs e)
        {
            patten._B();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button14_Click(object sender, EventArgs e)
        {
            patten.x();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button19_Click(object sender, EventArgs e)
        {
            patten._x();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button15_Click(object sender, EventArgs e)
        {
            patten.y();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button18_Click(object sender, EventArgs e)
        {
            patten._y();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button16_Click(object sender, EventArgs e)
        {
            patten.z();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button17_Click(object sender, EventArgs e)
        {
            patten._z();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button22_Click(object sender, EventArgs e)
        {
            patten.MU();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button21_Click(object sender, EventArgs e)
        {
            patten.MR();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button20_Click(object sender, EventArgs e)
        {
            patten.MF();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button25_Click(object sender, EventArgs e)
        {
            patten._MU();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button24_Click(object sender, EventArgs e)
        {
            patten._MR();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button23_Click(object sender, EventArgs e)
        {
            patten._MF();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
        }

        private void Button26_Click(object sender, EventArgs e)
        {
            times = 0;
            timer1.Enabled = true;
        }

        int times = 0;
        private void Timer1_Tick(object sender, EventArgs e)
        {
            if (times > 36) timer1.Enabled = false;
            patten.Random();
            project_painter.Draw();
            cube_painter.Draw();GC.Collect();
            times++;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        AI.IPattenMatch pattenMatch;
        List<string> steps = new List<string>();
        private void PattenMatch_PattenMatched(List<string> steps)
        {
            current_step = 0;
            this.steps = steps;
            timer2.Enabled = true;
        }

        private void Button27_Click(object sender, EventArgs e)
        {
            var cube_data = cube.Clone();
            Debug.WriteLine(cube_data == cube);
            pattenMatch = new AI.Cross(cube_data);
            pattenMatch.PattenMatched += PattenMatch_PattenMatched;
            pattenMatch.Invoke();
        }
        private void Button28_Click(object sender, EventArgs e)
        {
            var cube_data = cube.Clone();
            Debug.WriteLine(cube_data == cube);
            pattenMatch = new AI.FirstLayer(cube_data);
            pattenMatch.PattenMatched += PattenMatch_PattenMatched;
            pattenMatch.Invoke();
        }

        private void Button29_Click(object sender, EventArgs e)
        {
            var cube_data = cube.Clone();
            Debug.WriteLine(cube_data == cube);
            pattenMatch = new AI.SecondLayer(cube_data);
            pattenMatch.PattenMatched += PattenMatch_PattenMatched;
            pattenMatch.Invoke();
        }

        private void Button30_Click(object sender, EventArgs e)
        {
            var cube_data = cube.Clone();
            Debug.WriteLine(cube_data == cube);
            pattenMatch = new AI.OLL(cube_data);
            pattenMatch.PattenMatched += PattenMatch_PattenMatched;
            pattenMatch.Invoke();
        }

        private void Button31_Click(object sender, EventArgs e)
        {
            var cube_data = cube.Clone();
            Debug.WriteLine(cube_data == cube);
            pattenMatch = new AI.PLL(cube_data);
            pattenMatch.PattenMatched += PattenMatch_PattenMatched;
            pattenMatch.Invoke();
        }

        int current_step = 0;
        private void Timer2_Tick(object sender, EventArgs e)
        {
            if (current_step == steps.Count)
            {
                timer2.Enabled = false;
                steps.Clear();
                return;
            }
            patten.DoFromString(steps[current_step]);
            project_painter.Draw();
            cube_painter.Draw();
            current_step++;
        }

        private void Button32_Click(object sender, EventArgs e)
        {
            List<string> patten_str = new List<string>
            {
                "R","U'","R","U","R","U","R","U'","R'","U'","R","R"
            };
            foreach(var step in patten_str)
            {
                patten.DoFromString(step);
            }
            project_painter.Draw();
            cube_painter.Draw();
        }

        private void Button33_Click(object sender, EventArgs e)
        {
            var record = patten.GetRecord();
            var current_record_count = record.Count;
            for (var i = 0; i < current_record_count; i++)
            {
                patten.DoFromString(record[i]);
            }
            project_painter.Draw();
            cube_painter.Draw();
        }
    }
}
